
function getBranchStation(transferStationId) {
    $.getJSON("../stationManage_querySubStationByParentId.json",{"id":parseInt(transferStationId)},function (data) {
        if(data.success){
            $(".branchstation-list").append("<option value='-3'>所有支站点</option>");
            $.each(data.users,function (n,value) {
                $(".branchstation-list").append("<option value='"+value.id+"'>"+value.stationName+"</option>");
            });
        }
    });
}
function getTransferStation(city,area) {
    if(city=="")return;
    $.getJSON("../stationManage_findStationOfCity.json",{"city":city,"area":area},function (data) {
        if(data.success){

            $(".transferstation-list").append("<option value='-2'>所有中转站</option>");
            $.each(data.stations,function (n,value) {
                $(".transferstation-list").append("<option value='"+value.id+"'>"+value.stationName+"</option>");
            });
        }
    });
}
function getAllCity(parentId){
    $.getJSON("../address_query.json",{parentId : parentId},function(data){
        if(data.success && data.address != null){
            $(".city-list").append("<option value='-1'>所有城市</option>");
            $.each(data.address,function(index,item){
                $(".city-list").append("<option  value="+item.name+">"+item.name+"</option>");
            });
        }
    });
}
$(document).ready(function () {
    getAllCity(0);
    $(".city-list").change(function () {
        var area ="";
        if($(".city-list option:selected").text()=="所有城市"){
            var city =$(".city-list option:selected").val();
            $(".transferstation-list").val("").attr("disabled","disabled");
            $(".branchstation-list").val("").attr("disabled","disabled");
            getTransferStation(city,area);
        }else {
            $(".transferstation-list").removeAttr("disabled");
            $(".branchstation-list").removeAttr("disabled");
            $(".transferstation-list option:gt(0)").remove();
            $(".branchstation-list option:gt(0)").remove();
            var city = $(".city-list option:selected").val();
            getTransferStation(city,area);
        }
    });
    $(".transferstation-list").change(function () {
        if($(".transferstation-list option:selected").text()=="所有中转站"){
            transferStation = $(".transferstation-list option:selected").val();
            $(".branchstation-list").val("").attr("disabled","disabled");
            getBranchStation(transferStation);
        }else {
            $(".branchstation-list").removeAttr("disabled");
            $(".branchstation-list option:gt(0)").remove();
            transferStation = $(".transferstation-list option:selected").val();
            getBranchStation(transferStation);
        }

    });
    $(".btn-announce").click(function () {
        var notice_msg;
        var notice_city;
        var notice_station;
        var notice_title;
        var destStationId;
        notice_msg = $("textarea").val();
        city = $(".city-list option:selected").val();
        transferStation = $(".transferstation-list option:selected").val();
        branchStation = $(".branchstation-list option:selected").val();
        notice_title = $(".title").val();
        $.trim(notice_msg);
        if(branchStation!=""){
            destStationId=branchStation;
        }else if(transferStation != ""){
            destStationId=transferStation;
        }
        if(city==""){
            window.wxc.xcConfirm("请选择城市！", window.wxc.xcConfirm.typeEnum.warning);
        }else if(destStationId==""||destStationId==undefined){
            window.wxc.xcConfirm("请选择站点！", window.wxc.xcConfirm.typeEnum.warning);
        }else if(notice_msg==""){
            window.wxc.xcConfirm("请完善通知！", window.wxc.xcConfirm.typeEnum.warning);
        }else if(notice_title==""){
            window.wxc.xcConfirm("请完善通知！", window.wxc.xcConfirm.typeEnum.warning);
        }else {
            if(city=="-1"){
                destStationId=city;
                city="";
            }
            $.post("../noticeManage_sendNotice.json",{"title":notice_title,"content":notice_msg,"city":city,"midStationId":transferStation,"destStationId":destStationId},function (data) {
                if(data.success){
                    window.wxc.xcConfirm(data.msg, window.wxc.xcConfirm.typeEnum.success);
                    $(" textarea").val("");
                    $(".title").val("");
                }else {
                    window.wxc.xcConfirm(data.msg, window.wxc.xcConfirm.typeEnum.error);
                }
            },"json")
        }
    });
});